package xx.iverson.listening_manager.controller;

import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xx.iverson.listening_manager.entity.SysUser;
import xx.iverson.listening_manager.result.ReturnResult;
import xx.iverson.listening_manager.service.SysUserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * xx.iverson.listening_manager.controller.SysController
 * 描述：
 * on 2025/10/16-下午5:07
 *
 * @author weijx
 */
@RestController
@RequestMapping("/api/sys")
public class SysController {
    @Resource
    private SysUserService sysUserService;

    // 获取网站标题
    @PostMapping("/getTitle")
    public ReturnResult<String> getSiteTitle() {
        try {
            String title = sysUserService.selectTitle();
            return ReturnResult.success(title);
        } catch (Exception e) {
            return ReturnResult.error(500, "获取标题失败", e.getMessage());
        }
    }

    // 更新网站标题
    @PostMapping("/updateTitle")
    public ReturnResult<Boolean> updateSiteTitle(@RequestBody Map<String, String> request) {
        String title = request.get("title");
        try {
            if (title == null || title.trim().isEmpty()) {
                return ReturnResult.error(400, "标题不能为空");
            }
            boolean success = sysUserService.updateTitle(title.trim());
            return success ?
                    ReturnResult.success("标题更新成功", true) :
                    ReturnResult.error(500, "标题更新失败", false);
        } catch (Exception e) {
            return ReturnResult.error(500, "更新标题失败", false);
        }
    }

    // 用户登录
    @PostMapping("/login")
    public ReturnResult<SysUser> login(@RequestBody SysUser sysUser, HttpSession session) {
        try {
            String username = sysUser.getUsername();
            String password = sysUser.getPassword();

            if (username == null || password == null) {
                return ReturnResult.error(400, "用户名和密码不能为空");
            }

            SysUser user = sysUserService.selectByUsername(username);

            if (user == null || !user.getPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes()))) {
                return ReturnResult.error(401, "用户名或密码错误");
            }
            session.setAttribute("user", user);
            user.setPassword(null);
            return ReturnResult.success("登录成功", user);
        } catch (Exception e) {
            return ReturnResult.error(500, "登录失败");
        }
    }

    // 修改密码
    @PostMapping("/updatePassword")
    public ReturnResult<Boolean> updatePassword(@RequestBody Map<String, String> request) {
        try {
            String oldPassword = request.get("oldPassword");
            String newPassword = request.get("newPassword");

            if (oldPassword == null || newPassword == null) {
                return ReturnResult.error(400, "密码不能为空");
            }
            boolean success = sysUserService.updatePassword(oldPassword, newPassword);
            return success ?
                    ReturnResult.success("密码修改成功", true) :
                    ReturnResult.error(400, "原密码错误", false);
        } catch (Exception e) {
            return ReturnResult.error(500, "修改密码失败", false);
        }
    }

    // 退出登录
    @PostMapping("/logout")
    public ReturnResult<Boolean> logout(HttpSession session) {
        try {
            session.invalidate();
            return ReturnResult.success("退出成功", true);
        } catch (Exception e) {
            return ReturnResult.error(500, "退出失败", false);
        }
    }
}
